package com.cyy.AI_mbti.scoring;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyy.AI_mbti.model.dto.question.QuestionContentDTO;
import com.cyy.AI_mbti.model.entity.App;
import com.cyy.AI_mbti.model.entity.Question;
import com.cyy.AI_mbti.model.entity.ScoringResult;
import com.cyy.AI_mbti.model.entity.UserAnswer;
import com.cyy.AI_mbti.service.QuestionService;
import com.cyy.AI_mbti.service.ScoringResultService;
import lombok.RequiredArgsConstructor;
//import com.baomidou.mybatisplus.extension.toolkit.Db;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static io.lettuce.core.protocol.CommandKeyword.DB;

@RequiredArgsConstructor
@ScoringStrategyConfig(appType = 1, scoringStrategy = 0)
public class ScoreTestScoringStrategyImpl implements ScoringStrategy {
    private  final QuestionService questionService;
    private final ScoringResultService scoringResultService;
    @Override
    public UserAnswer doScore(List<String> choices, App app) throws Exception {
        Long appId = app.getId();
        //Db.lambdaQuery(Question.class);
        Question question = questionService.getOne(
                Wrappers.lambdaQuery(Question.class)
                        .eq(Question::getAppId, app.getId()));

        List<ScoringResult> scoringResultList = scoringResultService.list(
                Wrappers.lambdaQuery(ScoringResult.class)
                        .eq(ScoringResult::getAppId, app.getId()));
        Map<String, Integer> optionCount = new HashMap<>();
        List<QuestionContentDTO> questionDTOS = JSONUtil.toList(question.getQuestionContent(), QuestionContentDTO.class);
        for (int i=0;i<choices.size();i++) {
            String answer=choices.get(i);
            List<QuestionContentDTO.Option> options = questionDTOS.get(i).getOptions();
            for(QuestionContentDTO.Option option : options){
                if (option.getKey().equals(answer)) {
                    String result = option.getResult();
                    if (!optionCount.containsKey(result)) {
                        optionCount.put(result, 0);
                    }
                    optionCount.put(result, optionCount.get(result) + 1);
                }
            }
        }
    int maxScore = 0;
    ScoringResult maxScoringResult = scoringResultList.get(0);
    // 遍历评分结果列表
        for (ScoringResult scoringResult : scoringResultList) {
        List<String> resultProp = JSONUtil.toList(scoringResult.getResultProp(), String.class);
        // 计算当前评分结果的分数，[I, E] => [10, 5] => 15
        int score = resultProp.stream()
                .mapToInt(prop -> optionCount.getOrDefault(prop, 0))
                .sum();

        // 如果分数高于当前最高分数，更新最高分数和最高分数对应的评分结果
        if (score > maxScore) {
            maxScore = score;
            maxScoringResult = scoringResult;
        }
    }

    // 4. 构造返回值，填充答案对象的属性
    UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(appId);
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultId(maxScoringResult.getId());
        userAnswer.setResultName(maxScoringResult.getResultName());
        userAnswer.setResultDesc(maxScoringResult.getResultDesc());
        userAnswer.setResultPicture(maxScoringResult.getResultPicture());
        return userAnswer;
    }
}
